package com.sinopec.pi.scalaguide.classes

import scala.collection.mutable.ArrayBuffer

/**
 * 特质的使用
 */
trait Pet {
    val name: String
    def say(): Unit
}

class Cat(val name: String) extends Pet {
    override def say(): Unit = println(s"Cat: ${name}")
}
class Dog(val name: String) extends Pet {
    override def say(): Unit = println(s"Dog: ${name}")

}

object Pet {
    def main(args: Array[String]): Unit = {
        val dog = new Dog("Harry")
        val cat = new Cat("Sally")

        val animals = ArrayBuffer.empty[Pet]
        animals.append(dog)
        animals.append(cat)
        animals.foreach(pet => println(pet.name))  // Prints Harry Sally
        animals.foreach(pet => pet.say())
    }
}